////
////  ViewController.m
////  TestGizWits
////
////  Created by JM on 15/10/16.
////  Copyright © 2015年 JM. All rights reserved.
////
//
//#import "ViewController.h"
//#import <XPGWifiSDK/XPGWifiSDK.h>
//#import "UIAlertView+easyUse.h"
//
//#import <SystemConfiguration/CaptiveNetwork.h>
//
//#import "UIImageView+WebCache.h"
//#import "DYRequestAll.h"
//
////更新：2015-10-17
////类型：家电（白电）
////通信方式：WIFI
////Product Key: 576164bffa3545079f1c523e447ae405
//
//// App ID 和 Product Key
//static NSString * const IOT_APPKEY = @"2675d8b7962c413c86780fa88ed9ce6e";
//NSString * const IOT_PRODUCT       = @"3cbd3150a44a4412a26915dacb56b082";
// JM 灯App ID 和 Product Key
//
////晾衣架-A ios

//@interface ViewController ()<XPGWifiSDKDelegate,XPGWifiCentralControlDeviceDelegate>
//{
//    BOOL isDiscoverLock; //?
//}
//@property(nonatomic, copy)NSString *uid;
//@property(nonatomic, copy)NSString *token;
//@property (strong, nonatomic) NSArray *deviceList;
//@end
//
//@implementation ViewController
//
//- (void)viewDidLoad {
//    [super viewDidLoad];
//    // Do any additional setup after loading the view, typically from a nib.
//    [XPGWifiSDK startWithAppID:IOT_APPKEY];//这个ID是从产品信息里看到的 也就是设备被配的ID
//    [XPGWifiSDK setLogLevel:XPGWifiLogLevelAll logFile:nil printDataLevel:YES];
////    - (void)registerUser:(NSString *)userName password:(NSString *)password;
//}
//
//
//- (IBAction)dismissAllTextfeilds: (id) sender {
//    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
//}
//
//#pragma mark-注册
//- (IBAction)clickBtnResgine {
//        //调用注册方法
//    [XPGWifiSDK sharedInstance].delegate = self;
//    [[XPGWifiSDK sharedInstance] registerUser:@"JMTest2" password:@"123456"];
//}
//
////使用注册事件
//- (void)XPGWifiSDK:(XPGWifiSDK *)wifiSDK didRegisterUser:(NSNumber *)error errorMessage:(NSString *)errorMessage uid:(NSString *)uid token: (NSString *)token {
//    if([error intValue] == XPGWifiError_NONE)
//    {
//        NSLog(@"注册成功，可以成功获取到 uid:%@ 和 token:%@",uid,token);
//        //注册成功，可以成功获取到 uid 和 token
//    }
//    else
//    {
////        UIAlertView
//        [UIAlertView showMessage:errorMessage];
//        NSLog(@"注册失败，打印错误信息:%@:%@",error,errorMessage);
//        //注册失败，打印错误信息
//    }
//}
//#pragma mark-登陆
//- (IBAction)clickLogIn:(id)sender {
//    LoginRequest *request = [LoginRequest request];
//    request.DY_szUserName = self.nameTextField.text;
//    request.DY_szPassword = self.pwdTextFeild.text;
//    [request requstWithCompletionHandler:^(DYResponse * response, NSError * error) {
//        if (error) {
//            NSLog(@"登陆失败，打印错误信息:%@",error);
//            [UIAlertView showMessage:error.description];
//            return ;
//        }
//        LoginResponse * rs = (LoginResponse * )response;//需要加强转 控制代码，预防responese写错问题 todo
//    
//        NSLog(@"登陆成功，可以成功获取到 uid:%@ 和 token:%@",rs.DY_uid,rs.DY_token);
//        self.uid = rs.DY_uid;
//        self.token = rs.DY_token;
//        [rs print];
//    }];
//}
//
//#pragma mark-注销
//- (IBAction)clickLogout:(id)sender {
//    [XPGWifiSDK sharedInstance].delegate = self;
//    [[XPGWifiSDK sharedInstance] userLogout:self.uid];
//    
//}
////使⽤注销事件
//- (void)XPGWifiSDK:(XPGWifiSDK *)wifiSDK didUserLogout:(NSNumber *)error errorMessage:(NSString *)errorMessage
//{
//    if([error intValue] == XPGWifiError_NONE)
//    {
//          NSLog(@"注销成功");
//        //注销成功
//    }
//    else
//    {
//        [UIAlertView showMessage:errorMessage];
//        NSLog(@"登陆失败，打印错误信息:%@:%@",error,errorMessage);
//        //注销失败
//    }
//}
//
////修改密码 略 ／过云端给邮箱发送重置密码链接修改密码 ／匿名用户转换实名用户¶。。。各种转
//
//#pragma mark- 检出ssid 设备在用的
//- (NSString *)ssidInUsing {
//    NSArray *ifs = (__bridge_transfer NSArray *)CNCopySupportedInterfaces();
//    NSDictionary *info = nil;
//    NSLog(@"ifs:%@",ifs);
//
//    for (NSString *ifnam in ifs) {
//        info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
//        if (info && [info count])
//        {
//            break;
//        }
//    }
//    NSLog(@"surrent ssid info:%@",info);
//
//    
//    NSString *ssid = [info objectForKey:@"SSID"];
//    if([ssid hasPrefix:@"XPG-GAgent"])
//    {     
//        //是 Soft-AP 模式
//    }
//    return ssid;
//}
//
//-(IBAction)clickCheckOutSsid:(id)sender; {
//    NSString *ssid = [self ssidInUsing];
//    self.ssidTextFeild.text = ssid;
//    
//    NSDictionary *dic = @{@1:@"Soft-AP 模式",@2:@"AirLink 模式"};
//    if([ssid hasPrefix:@"XPG-GAgent"])
//    {
////        XPGWifiSDKSoftAPMode
//        //是 Soft-AP 模式
//        self.OnLineModeTextFeild.text = dic[@1];
//    }else {
//        self.OnLineModeTextFeild.text = dic[@2];
//    }
//    
//}
//
//#pragma mark-4.1 最重要的 配置设备上线（onBoarding)
//- (IBAction)clickSetDeviceOnLine:(id)sender {
//    
//    [XPGWifiSDK sharedInstance];
//    
//    //Soft-AP 模式 on-boarding 的方法
//    [XPGWifiSDK sharedInstance].delegate = self;
//    [[XPGWifiSDK sharedInstance] setDeviceWifi:self.ssidTextFeild.text key:self.pwdTextFeild.text mode:XPGWifiSDKSoftAPMode softAPSSIDPrefix:@"XPG-GAgent" timeout:60];
//    
//    NSNumber *modeObj = @{@"Soft-AP 模式":@1,@"AirLink 模式":@2}[self.OnLineModeTextFeild.text];
//    if (modeObj.intValue == 1) {
//         [[XPGWifiSDK sharedInstance] setDeviceWifi:self.ssidTextFeild.text key:self.pwdOfSsidTextFeild.text mode:XPGWifiSDKSoftAPMode softAPSSIDPrefix:@"XPG-GAgent" timeout:15 wifiGAgentType:@[@(XPGWifiGAgentTypeHF)]];
//    } else if (modeObj.intValue == 2) {
//        [[XPGWifiSDK sharedInstance] setDeviceWifi:self.ssidTextFeild.text key:self.pwdOfSsidTextFeild.text mode:XPGWifiSDKAirLinkMode softAPSSIDPrefix:nil timeout:30 wifiGAgentType:@[@(XPGWifiGAgentTypeHF)]];
//        //@[(XPGWifiGAgentTypeHF)]//这个参数 设置错误 不会有任何提示
//    } else {
//        [UIAlertView showMessage:@"请检查代码！！"];
//    }
//    
////    [[XPGWifiSDK sharedInstance] setDeviceWifi:self.ssidTextFeild.text key:self.pwdTextFeild.text mode:modeObj.intValue softAPSSIDPrefix:@"XPG-GAgent" timeout:60 wifiGAgentType:@[@""]];
//}
//
////配置事件
//- (void)XPGWifiSDK:(XPGWifiSDK *)wifiSDK didSetDeviceWifi:(XPGWifiDevice *)device result:(int)result {
//    if(result == XPGWifiError_NONE)
//    {
//         [UIAlertView showMessage:@"配置成功"];
//        NSLog(@"配置成功 设备：%@",device);
//        //配置成功
//    }
//    else
//    {
//        //密码错误，连接超时 都会报－40超时错误。
//        [UIAlertView showMessage:@"配置失败"];
//         NSLog(@"配置失败 result:%d",result);
//        //配置失败
//    }
//}
//
//////Soft-AP 模式 on-boarding 的方法
////[XPGWifiSDK sharedInstance].delegate = self;
////[[XPGWifiSDK sharedInstance] setDeviceWifi:@"ssid-1" key:@"12345678" mode:XPGWifiSDKSoftAPMode softAPSSIDPrefix:@"XPG-GAgent" timeout:60];
//- (IBAction)requestDeviceList:(id)sender {
//    
//    
//    //AirLink 模式 on-boarding 的方法
//    [XPGWifiSDK sharedInstance].delegate = self;
//    
//    //防止操作中点了以后卡主线程。
//    if(isDiscoverLock)
//        return;
//    
//    //需要开发者自行把 uid 和 token 换成实际的值
//    [[XPGWifiSDK sharedInstance] getBoundDevicesWithUid:self.uid token:self.token specialProductKeys:nil];
//    
//}
////搜索设备
//- (void)XPGWifiSDK:(XPGWifiSDK *)wifiSDK didDiscovered:(NSArray *)deviceList result:(int)result {
//    if(isDiscoverLock)
//        return;
//    NSLog(@"设备列表:%@",deviceList);
//    //设置设备列表
//    self.deviceList = deviceList;
//    self.deviceListTextView.text = [NSString stringWithFormat:@"%@",deviceList];
//}
//- (IBAction)clickBlindDevice:(id)sender {
//    //需要开发者自行把 uid 和 token 换成实际的值//虚拟设备，我passCode我 写123456 才能过以后换换别的试试
//    [[XPGWifiSDK sharedInstance] bindDeviceWithUid:self.uid    token:self.token did:@"JGWbLhCe9ZTmRLezCwPxGy" passCode:@"123456" remark:nil];
//    
//}
////绑定事件
//- (void)XPGWifiSDK:(XPGWifiSDK *)wifiSDK didBindDevice:(NSString *)did error:(NSNumber *)error errorMessage:(NSString *)errorMessage {
//    if([error intValue] == XPGWifiError_NONE)
//    {
//        NSLog(@"绑定成功");
//        //绑定成功
//    }
//    else
//    {
//        NSLog(@"绑定失败:%@:%@",error,errorMessage);
//        //绑定失败
//    }
//}
//- (IBAction)clickSendMsg:(id)sender {
//    
//    
//    XPGWifiDevice *device = self.deviceList[0];
//    device.delegate = self;
//    [device write:@{@"entity0":@{@"switch":@YES},
//                    @"cmd":@1}];
//}
///**
//子设备列表的回调接口，返回发现子设备设备的结果
//@param wifiCentralControlDevice 返回触发回调的中控设备实例
//@param subDeviceList 为 XPGWifiSubDevice* 实例数组
//@param result 0为成功，其他失败
//@see 触发函数：[XPGWifiCentralControlDevice getSubDevices]
//*/
//- (void)XPGWifiCentralControlDevice:(XPGWifiCentralControlDevice *)wifiCentralControlDevice
//                      didDiscovered:(NSArray *)subDeviceList
//                             result:(int)result {
//    
//    NSLog(@"不知道什么作用XPGWifiCentralControlDevice");
//    
//}
//
//
//- (IBAction)clickConnectLogInDevice:(id)sender {
//    
//    //得到设备实例
//    XPGWifiDevice *device = self.deviceList[0];
//    
//    //如果没有设置委托，则设置委托到当前使用的Cocoa类所对应的实例
//    device.delegate = self;
//    
//    //需要开发者自行把 uid 和 token 换成实际的值
//    [device login:self.uid token:self.token];
//    
//}
//
//- (void)XPGWifiDevice:(XPGWifiDevice *)device didLogin:(int)result
//{
//    if(result == XPGWifiError_NONE)
//    {
//        self.receiveMsgTextView.text = [NSString stringWithFormat:@"登陆成功"];
//        //登录成功
//    }
//    else
//    {
//        self.receiveMsgTextView.text = [NSString stringWithFormat:@"登陆失败：%d",result];
//        //登录失败
//    }
//}
//
//
//- (void)XPGWifiDevice:(XPGWifiDevice *)device didReceiveData:(NSDictionary *)data result:(int)result {
//    self.receiveMsgTextView.text = [NSString stringWithFormat:@"%@",data];
//    
//    // retsult= -20  当没有配置文件的时候，会报这个错误。
//    
//    //基本数据，与发送的数据格式⼀一致
//    //    NSDictionary *_data = [data valueForKey:@"data"];
//    //
//    //    //警告
//    //    NSArray *alarms = [data valueForKey:@"alarms"];
//    //
//    //    //错误
//    //    NSArray *faults = [data valueForKey:@"faults"];
//    //
//    //    //透传数据
//    //    NSDictionary *binary = [data valueForKey:@"binary"];
//}
//
//#pragma mark-获取配置文件
//
//- (IBAction)clickRequestConfig:(id)sender {
//    [XPGWifiSDK sharedInstance].delegate = self;
//    [XPGWifiSDK updateDeviceFromServer:IOT_PRODUCT];
//    //会存储配置文件XXX.json到 此目录下。
//    ///Users/JM/Library/Developer/CoreSimulator/Devices/3C92E8E8-1577-4907-9B70-C9E42E163BFD/data/Containers/Data/Application/EE849BFC-82C7-4051-AB77-7ACFE67988FC/Documents/XPGWifiSDK/Devices
//}
//
//
//- (void)XPGWifiSDK:(XPGWifiSDK *)wifiSDK didUpdateProduct:(NSString *)product result:(int)result
//{
//    if([product isEqualToString:IOT_PRODUCT])
//    {
//        //说明下载的是这个产品
//    }
//    if(result == XPGWifiError_NONE)
//    {
//        NSLog(@"下载配置成功：%@,%d",product,result);
//        //下载配置成功
//    }
//    else
//    {
//        NSLog(@"下载配置失败:%d",result);
//        //下载配置失败
//    }
//}
//
//
//- (IBAction)touch:(id)sender {
//    [self.view endEditing:YES];
//}
//
//#pragma mark- 额外复杂流程  手机号注册
//- (IBAction)requestSecretCodeImage:(id)sender {
//    //调用获取图片验证码方法
//    [XPGWifiSDK sharedInstance].delegate = self;
//    [[XPGWifiSDK sharedInstance] getCaptchaCode:IOT_appSecret];
//}
//
////使用获取图片验证码事件
//- (void)wifiSDK:(XPGWifiSDK *)wifiSDK didGetCaptchaCode:(NSError *)result token:(NSString *)token captchaId:(NSString *)captchaId captchaURL:(NSString *)captchaURL
//{
//    if(result.code == XPGWifiError_NONE) {
//        [UIAlertView showMessage:@"获取图片验证码成功"];
//        //获取图片验证码成功，下载验证码图片
//        NSURL *url = [NSURL URLWithString:captchaURL];
////        NSData *data = [NSData dataWithContentsOfURL:url];
////        if(data)
////        {
////            //把验证码图片显示在 UI 上
////            UIImage *verifyImage = [UIImage imageWithData:data];
////            self.secretCodeImage.image = verifyImage;
////        }
//        [self.secretCodeImage sd_setImageWithURL:url];
//        self.SMStoken = token;
//        self.captchaId = captchaId;
//    }
//    else
//    {
//         [UIAlertView showMessage:@"获取图片验证码失败"];
//        //获取图片验证码失败
//    }
//}
//
//- (IBAction)requestSendSMS:(id)sender {
//    
//    RegitsterSendSMSRequest *request = [[RegitsterSendSMSRequest alloc]init];
//    request.DY_token = self.SMStoken;
//    request.DY_captchaId = self.captchaId;
//    request.DY_captchaCode = self.secreCodeTextField.text;
//    request.DY_phone = @"13512170376";
//    [request requstWithCompletionHandler:^(DYResponse *reponse, NSError * error) {
//        if (error) {
//            [UIAlertView showMessage:[NSString stringWithFormat:@"验证码获取失败%@",error]];
//        } else {
//            [UIAlertView showMessage:@"获取手机验证码成功"];
//            //发送成功，开启60秒计时
//        }
//    }];
//}
//@end
